Skip to content

Conversation

prestonvasquez
Copy link
Member

GODRIVER-3436

Summary

If the value reader is null, prevent DefaultValueDecoders from initializing a pointer to the zero value.

Background & Motivation

The current behavior will lead to unexpected results:

ill instantiate a pointer field decoded from null data if the user defines a UnmarshalBSONValue. For example:

package main

import (
	"fmt"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/bsontype"
	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)

type DBInt64 int64

type Product struct {
	TotalForSell *DBInt64 `json:"total_for_sell" bson:"total_for_sell,omitempty"`
}

func (i *DBInt64) UnmarshalBSONValue(t bsontype.Type, value []byte) error {
	return nil
}

func main() {
	idx, doc := bsoncore.AppendDocumentStart(nil)
	doc = bsoncore.AppendNullElement(doc, "total_for_sell")

	doc, err := bsoncore.AppendDocumentEnd(doc, idx)
	if err != nil {
		panic(err)
	}

	bytes := bson.Raw(doc)

	got := Product{}
	if err := bson.Unmarshal(bytes, &got); err != nil {
		panic(err)
	}

	if got.TotalForSell != nil {
		fmt.Println("null value decoded as non-nil")
	}
}

Output:

❯ go run custom_type_with_pointer.go
null value decoded as non-nil

@prestonvasquez prestonvasquez added review-priority-urgent High Priority PR for Review: review immediately! review-priority-normal Medium Priority PR for Review: within 1 business day review-priority-low Low Priority PR for Review: within 3 business days and removed review-priority-urgent High Priority PR for Review: review immediately! review-priority-normal Medium Priority PR for Review: within 1 business day labels Dec 5, 2024
Copy link
Contributor

API Change Report

No changes found!

@prestonvasquez prestonvasquez added review-priority-urgent High Priority PR for Review: review immediately! and removed review-priority-low Low Priority PR for Review: within 3 business days labels Dec 6, 2024
Copy link
Collaborator

@qingyang-hu qingyang-hu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@prestonvasquez prestonvasquez merged commit 6c22d67 into mongodb:v1 Dec 6, 2024
31 of 34 checks passed
@prestonvasquez prestonvasquez deleted the GODRIVER-3436 branch December 6, 2024 18:33
@prestonvasquez
Copy link
Member Author

drivers-pr-bot please backport to release/1.17

alcaeus added a commit that referenced this pull request Mar 4, 2025
* commit '0dc2e05e': (184 commits)
  GODRIVER-3448 Limit GOMAXPROCS for fuzz tests (#1939) [v1] (#1943)
  GODRIVER-3470 Correct BSON unmarshaling logic for null values (#1924)
  GODRIVER-3370 Add bypassEmptyTsReplacement option. (#1927)
  BUMP v1.17.2
  GODRIVER-3436 Avoid initializing null data given custom decoder (#1902)
  GODRIVER-3340 Add a test for goroutine leaks. (#1874)
  Update reviewers.txt (#1855) [v1] (#1883)
  Fix data race in 'discard connections' pool test. [v1] (#1877)
  Bump golangci-lint for 1.23 compatibility [v1] (#1875)
  GODRIVER-3340 Bump github.com/klauspost/compress from 1.13.6 to 1.16.7 [v1] (#1869)
  GODRIVER-3374 Add ReadCompressedCompressedMessage back to wiremessage API (#1870)
  GODRIVER-3156 Detect and discard closed idle connections. (#1815)
  GODRIVER-3358 Do not override authSource from TXT record (#1830)
  DEVPROD-10453 Use assume_role for s3 uploads [v1] (#1824)
  GODRIVER-2589 Clarify `*Cursor.All()` behavior in comment. (#1804)
  GODRIVER-3313 Skip CSOT spec tests on Windows and macOS. (#1818)
  BUMP v1.17.0
  GODRIVER-3302 Handle malformatted message length properly. (#1758)
  GODRIVER-3312 Use remaining test secrets from the vault [v1] (#1811)
  Remove GCP from supplied callback example (#1809)
  ...
alcaeus added a commit that referenced this pull request Mar 4, 2025
…41094483929

* release/2.0: (203 commits)
  Use different credentials for merge-up PRs (#1968)
  Add GitHub Actions workflow for merge ups (#1962)
  BUMP v1.17.3
  GODRIVER-3448 Limit GOMAXPROCS for fuzz tests (#1939) [v1] (#1943) [release/1.17] (#1957)
  Update reviewers.txt (#1855) [v1] (#1883) [release/1.17] (#1958)
  Cherry pick 1.17.2 work to release/1.17 (#1956)
  GODRIVER-3470 Correct BSON unmarshaling logic for null values (#1924) (#1955)
  GODRIVER-3370 Add bypassEmptyTsReplacement option. (#1927) [release/1.17] (#1954)
  GODRIVER-3448 Limit GOMAXPROCS for fuzz tests (#1939) [v1] (#1943)
  GODRIVER-3470 Correct BSON unmarshaling logic for null values (#1924)
  GODRIVER-3370 Add bypassEmptyTsReplacement option. (#1927)
  BUMP v1.17.2
  GODRIVER-3436 Avoid initializing null data given custom decoder (#1902)
  GODRIVER-3340 Add a test for goroutine leaks. (#1874)
  Update reviewers.txt (#1855) [v1] (#1883)
  GODRIVER-3340 Bump github.com/klauspost/compress from 1.13.6 to 1.16.7 [release/1.17] (#1880)
  GODRIVER-3374 Add ReadCompressedCompressedMessage back to wiremessage API (#1870)
  Fix data race in 'discard connections' pool test. [v1] (#1877)
  Bump golangci-lint for 1.23 compatibility [v1] (#1875)
  Fix data race in 'discard connections' pool test. [v1] (#1877)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
review-priority-urgent High Priority PR for Review: review immediately!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants